// Created by Roman Novitsky on 29.04.2009


#region Imported Namespaces


using System;

using WtfManager.UIManager;


#endregion


namespace WtfManager.FileManager
{
  public class GenericFileSystemItemFilter: ICollectionViewFilter
  {
    #region Fields


    private string _propertyName;


    #endregion


    #region Properties/Indexers


    public Predicate<object> Filter
    {
      get
      {
        switch (PropertyName)
        {
          case "IsHidden":
            return CheckIsHidden;

          case "IsFile":
            return CheckIsFile;
        }
        return null;
      }
    }

    public string PropertyName
    {
      get
      {
        return _propertyName;
      }
      set
      {
        if (value == "IsHidden" || value == "IsFile")
        {
          _propertyName = value;
        }
        else
        {
          throw new ArgumentException("Invalid property name specified.");
        }
      }
    }

    public object Value
    {
      get;
      set;
    }


    #endregion


    #region Methods


    private bool CheckIsHidden(object item)
    {
      if (item is FileSystemItem)
      {
        var fileSystemItem = (FileSystemItem)item;
        return fileSystemItem.IsHidden == (bool)Value;
      }

      return true;
    }


    private bool CheckIsFile(object item)
    {
      if (item is FileSystemItem)
      {
        var fileSystemItem = (FileSystemItem)item;
        return fileSystemItem.IsFile == (bool)Value;
      }

      return true;
    }


    #endregion
  }
}